﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;

namespace DNA.Mvc.jQuery
{
    public class ToolbarButtonFactory : ViewComponentBuilderFactory<ViewComponent>
    {
        public ToolbarButtonFactory(Toolbar component, ViewContext context, IViewDataContainer container) : base(component, context, container) { }
        private int btnIndex = 0;
        private int comboIndex = 0;

        public ToolbarButtonBuilder Add()
        {
            btnIndex++;
            var button = new ToolbarButton() { Name = ((ViewComponent)Container).Id +"button"+ btnIndex.ToString() };
            this.AddItem(button);
            var builder = new ToolbarButtonBuilder(button, this.ViewContext,this.ViewDataContainer);
            return builder;
        }

        public ToolbarButtonBuilder AddButton(string text)
        {
            return Add().Text(text);
        }

        public ToolbarButtonBuilder AddButton(string text, string onclick)
        {
            return Add().Text(text).Click(onclick);
        }

        public ToolbarButtonBuilder AddImageButton(string imageUrl, string onclick)
        {
            return this.Add().ImageIcons(imageUrl).Click(onclick);
        }
        
        public ToolbarButtonBuilder AddIconButton(string cssClass, string onclick)
        {
            return this.Add().Icons(cssClass).Click(onclick);
        }

        public ToolbarDropdownBuilder AddDropdown(string text)
        {
            btnIndex++;
            var button = new ToolbarButton() { Name = ((ViewComponent)Container).Id + "button" + btnIndex.ToString() };
            this.AddItem(button);
            var builder = new ToolbarDropdownBuilder(button, this.ViewContext,this.ViewDataContainer);
            builder.Text(text);

            return builder;
        }

        public ToolbarButtonBuilder AddLink(string text, string navigateUrl)
        {
            return Add().Text(text).NavigateUrl(navigateUrl);
        }

        private void AddItem(ViewComponent item)
        {
            Container.Items.Add(item);
            Container.OnItemAdded(item);
        }

        public void AddLabel(string text)
        {
            this.AddItem(new ToolbarLabel() { Text = text });
        }

        public void AddSpliter()
        {
            var spliter = new ToolbarSpliter();
            this.AddItem(spliter);
        }

        public ComboBoxBuilder<T> AddCombo<T>(IEnumerable<T> model) 
            where T:class
        {
            comboIndex++;
            var _AjaxHelper = new AjaxHelper<T>(this.ViewContext, this.ViewDataContainer);
            var combo = _AjaxHelper.DnaFor().ComboBox(model);
            combo.Name(((ViewComponent)Container).Id + "combo" + comboIndex.ToString());
            this.Add().Template(() =>
            {
                combo.Render();
            });
            return combo;
        }

        public ComboBoxBuilder<T> AddCombo<T>(IEnumerable<T> model,Expression<Func<T, dynamic>> textFieldSelector,
            Expression<Func<T, dynamic>> valueFieldSelector)
    where T : class
        {
            comboIndex++;
            var _AjaxHelper = new AjaxHelper<T>(this.ViewContext, this.ViewDataContainer);
            var combo = _AjaxHelper.DnaFor().ComboBox(model,textFieldSelector,valueFieldSelector);
            combo.Name(((ViewComponent)Container).Id + "combo" + comboIndex.ToString());
            this.Add().Template(() =>
            {
                combo.Render();
            });
            return combo;
        }

        public ComboBoxBuilder AddCombo()
        {
            var _helper = new AjaxHelper(this.ViewContext, this.ViewDataContainer);
            comboIndex++;
            var combo = _helper.Dna().ComboBox(((ViewComponent)Container).Id + "combo" + comboIndex.ToString());
            this.Add().Template(() =>
            {
                combo.Render();
            });
            return combo;
        }
    }

}
